#include "font.h"
#include "lcd.h"

extern unsigned char fontdata_8x16[];
extern unsigned char fontdata_16x16_chinese[][32];

static unsigned int fb_base;
static int xres, yres, bpp;
 
 
static void fb_font_get_lcd_params(void)
{
    get_lcd_params(&fb_base,  &xres, &yres, &bpp);
}


void fb_print_chinese(int x, int y, int num,  unsigned int color)
{
    int i,j;

    fb_font_get_lcd_params();

    unsigned char *dots = (unsigned char *)&fontdata_16x16_chinese[num - 1];

    unsigned char data;

    int bit;
        for(j = y; j < y + 16; j++)
        {
            data = *dots++;
            bit = 7;
            for(i = x; i < x + 8; i++)
            {
                if (data & (1 << bit))
                    fb_draw_pixel(i, j, color);
        
                bit--;
            }
            data = *dots++;
            bit = 7;
            for(i = x + 8; i < x + 16; i++)
            {
                if (data & (1 << bit))
                    fb_draw_pixel(i, j, color);
        
                bit--;
            }

        }
}


/* 根据字母的点阵在LCD上描画文字 */

void fb_print_char(int x, int y, char c, unsigned int color)
{
    int i, j;

    /* 根据c的ascii码在fontdata_8x16中得到点阵数据 */

    fb_font_get_lcd_params();
    unsigned char *dots = &fontdata_8x16[c * 16];

    /* 根据点阵来设置对应象素的颜色 */
    unsigned char data;
    int bit;

    /* 根据点阵来设置对应象素的颜色 */
    for (j = y; j < y+16; j++)
    {
        data = *dots++;
        bit = 7;
        for (i = x; i < x+8; i++)
        {
           /* 根据点阵的某位决定是否描颜色 */
           if (data & (1<<bit))
               fb_draw_pixel(i, j, color);
           bit--;
        }
    }
}


void fb_print_string(int x, int y, char *str, unsigned int color)
{
    int i = 0, j;

    while(str[i])
    {
        if (str[i] == '\n')
            y = y + 16;
        else if (str[i] == '\r')
            x = 0;
        else
        {
            fb_print_char(x, y, str[i], color);
            x = x + 8;
            if (x >= xres)  /* 换行  */
            {
                x = 0;
                y += 16;
            }
        }
        i++;
    }
}
